package service

import (
	"fmt"
	"learn/global"
	"learn/pkg/e"
	util "learn/pkg/utils/log"
	"learn/respository/db/dao"
	"learn/serializer"
	"sync"

	"learn/forms"
	"learn/respository/db/model"
)

var (
	pmsBrandServiceInstance *PmsBrandService
	once                    sync.Once
)

type PmsBrandService struct {
}

func NewPmsBrandService() *PmsBrandService {
	once.Do(func() {
		pmsBrandServiceInstance = &PmsBrandService{}
	})
	return pmsBrandServiceInstance
}

func (service *PmsBrandService) Create(req *forms.PmsBrandForm) serializer.Response {

	util.LogrusObj.Infoln("PmsBrandService.Create. param:%v", req)
	code := e.SUCCESS
	brand := model.PmsBrand{
		Name:                req.Name,
		FirstLetter:         req.FirstLetter,
		Sort:                req.Sort,
		FactoryStatus:       req.FactoryStatus,
		ShowStatus:          req.ShowStatus,
		ProductCount:        req.ProductCount,
		ProductCommentCount: req.ProductCommentCount,
		Logo:                req.Logo,
		BigPic:              req.BigPic,
		BrandStory:          req.BrandStory,
	}
	pmsBrandDao := dao.NewPmsBrandDao()
	err := pmsBrandDao.Create(&brand)
	if err != nil {
		code = e.ErrorDatabase
		util.LogrusObj.Errorln("PmsBrandService.Create. param:%v,error:", req, err)
	}
	global.DB.Save(brand)

	return serializer.Response{
		Status: code,
		Msg:    e.GetMsg(code),
		Data:   brand,
	}

}

func (service *PmsBrandService) PageList(keywords string, showStatus, pageNum, pageSize int) serializer.Response {

	util.LogrusObj.Infoln(fmt.Sprintf("PmsBrandService.PageList. keywords:%s,showStatus:%d,pageNum:%d,pageSize:%d", keywords, showStatus, pageNum, pageSize))
	pmsBrandDao := dao.NewPmsBrandDao()
	pmsBrandList, total, _ := pmsBrandDao.PageList(keywords, showStatus, pageNum, pageSize)

	return serializer.BuildListResponse(pmsBrandList, uint(*total))

}
